package com.team.cloudsharing.dao.user;

import java.io.Serializable;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.team.cloudsharing.ultis.Constant;

@Repository
public class UserDAOImpl implements UserDAO{
	
	final static Logger logger = Logger.getLogger(UserDAOImpl.class);

	@Autowired
	SessionFactory sessionFactory;
	
	@Override
	@Transactional
	public long insertUser(UserDO userDO) {
		long userId = -1;
		Session session = null;
		if (userDO != null) {
			try {
				session = sessionFactory.openSession();
				Transaction tx = session.beginTransaction();
				session.saveOrUpdate(userDO);
				tx.commit();
				Serializable id = session.getIdentifier(userDO);
				userId = (Long) id;
			} catch (Exception e) {
				logger.error(e.getMessage());
			} finally {
				try {
					session.close();
				} catch (Exception e2) {
					logger.error(e2.getMessage());
				}
			}
		}
		return userId;
	}

	@Override
	@Transactional
	public long updateUser(UserDO userDO) {
		long statusUpdate = -1;
		Session session = null;
		if (userDO != null) {
			try {
				session = sessionFactory.openSession();
				Transaction tx = session.beginTransaction();
				session.saveOrUpdate(userDO);
				tx.commit();
				Serializable id = session.getIdentifier(userDO);
				statusUpdate = (Long) id;
			} catch (Exception e) {
				logger.error(e.getMessage());
			} finally {
				try {
					session.close();
				} catch (Exception e2) {
					logger.error(e2.getMessage());
				}
			}
		}
		return statusUpdate;
	}

	@Override
	public UserDO findUserById(long userId) {
		UserDO userDO = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			session.beginTransaction();
			userDO = (UserDO) session.get(UserDO.class, userId);
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return userDO;
	}

	@Override
	@Transactional
	public boolean deleteUser(long userId) {
		boolean statusDelete = false;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			UserDO userDO = (UserDO) session.load(UserDO.class, userId);
			Transaction tx = session.getTransaction();
			session.beginTransaction();
			session.delete(userDO);
			tx.commit();
			statusDelete = true;
		} catch (Exception e) {
			statusDelete = false;
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return statusDelete;
	}
	
	@Override
	public long findUserIdByUserEmailAndPassword(String userEmail,	String password) {
		long returnValue = Constant.WRONG_PASSWORD;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(UserDO.class);
			criteria.add(Restrictions.eq("userEmail", userEmail));
			UserDO userDO = (UserDO) criteria.uniqueResult();
			if (userDO != null) {
				if (userDO.isActivated() == false) {
					returnValue = Constant.NOT_ACTIVATE;
				} else if (userDO.getPassword().equals(password)) {
					returnValue = userDO.getUserId();
				}
			} else {
				returnValue = Constant.NOT_REGISTER;
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return returnValue;
	}
	
	@Override
	public UserDO findUserByUserEmail(String userEmail) {
		UserDO userDOReturn = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(UserDO.class);
			criteria.add(Restrictions.eq("userEmail", userEmail.trim()));
			userDOReturn = (UserDO) criteria.uniqueResult();
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return userDOReturn;
	}

	@Override
	public String getUserEmailByUserId(long userId) {
		UserDO userDO = null;
		Session session = null;
		try {
			session = sessionFactory.openSession();
			Criteria criteria = session.createCriteria(UserDO.class);
			criteria.add(Restrictions.eq("userId", userId));
			userDO = (UserDO) criteria.uniqueResult();
		} catch (Exception e) {
			logger.error(e.getMessage());
		} finally {
			try {
				session.close();
			} catch (Exception e2) {
				logger.error(e2.getMessage());
			}
		}
		return userDO.getUserEmail();
	}
}
